import hashlib
import socket
import base64
from redash import settings
from datetime import datetime


def daz_uname():
    host = socket.getfqdn(socket.gethostname())
    addr = socket.gethostbyname(host)
    return host + addr + settings.SQLALCHEMY_DATABASE_URI + settings.DAZ_USERS + settings.DAZ_DATE

def daz_short_uname():
    return settings.SQLALCHEMY_DATABASE_URI + settings.DAZ_USERS + settings.DAZ_DATE

def daz_hash_password(symbol, pws):
    metastr = symbol + "728304" + daz_short_uname()
    shastr = metastr.encode('gbk')
    sha = hashlib.sha1(shastr).hexdigest()
    if sha != pws:
        metastr = symbol + "728304" + daz_uname()
        shastr = metastr.encode('gbk')
        sha = hashlib.sha1(shastr).hexdigest()    
    # metastr = symbol + "728304" + 'CM192.192.194.235postgresql:///dazcontract02021-03-31'
    # shastr = metastr.encode('gbk')
    # sha = hashlib.sha1(shastr).hexdigest()
    return True if sha == pws else False 

def daz_password():
    if not daz_hash_password("COMMON",settings.DAZ_COMMON): return False

    if settings.DAZ_DATE == "": return True

    today = datetime.now().date()
    expire = datetime.strptime(settings.DAZ_DATE, '%Y-%m-%d').date()
    if today>expire : return False
    return True
